<?php
namespace Home\Controller;
use Think\Controller;
class OpenController extends Controller {


   //定时任务返还的访问
    public function index()
    {     
            $cur_time=time();
            //下单之后返还财富积分
            $orderLists=M('order')->where('paystatus=1')->select();
          //  $moshi=M('moshi')->where('status=1')->find();
            foreach ($orderLists as $key => $value) {
                   for ($i=1; $i <= $value['moshi_week1_num']; $i++) { 
                        $cur_timeime1=strtotime("+".$i."week",$value['audit_time']);
                        $cur_timeime2=$cur_timeime1+86400;
                        if (time()>=$cur_timeime1 && time()<=$cur_timeime2) {
                            $add_cf=$value['moshi_cf_week']*$value['order_num'];
                            M('user')->where("id=".$value['user_id'])->setInc('moshi_yfh_cf',$add_cf);

                            M('user')->where("id=".$value['user_id'])->setInc('cf_jifen',$add_cf);

             $record['user_id']=$value['user_id'];
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$add_cf;
             $record['addtime']=time();
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                        }

                    }

                    for ($i=1; $i <= $value['moshi_week2_num']; $i++) { 
                        $cur_timeime1=strtotime("+".$i."week",$value['audit_time']);
                        $cur_timeime2=$cur_timeime1+86400;
                        if (time()>=$cur_timeime1 && time()<=$cur_timeime2) {
                            $add_cf=$value['moshi_xf_week']*$value['order_num'];

                            M('user')->where("id=".$value['user_id'])->setInc('cf_jifen',$add_cf);

                            M('user')->where("id=".$value['user_id'])->setInc('xf_jifen',$add_cf);
                            M('user')->where("id=".$value['user_id'])->setInc('moshi_yfh_cf',$add_cf);
                            M('user')->where("id=".$value['user_id'])->setInc('moshi_yfh_xf',$add_cf);
                            unset($record);
                             $record['user_id']=$value['user_id'];
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$add_cf;
             $record['addtime']=time();
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

             $record2['user_id']=$value['user_id'];
             $record2['items']='每周返还消费积分';
             $record2['jifen_num']='+'.$add_cf;
             $record2['addtime']=time();
             $record2['jifen_type']='xf';
             M('jifen_record')->add($record2);

                        }

                        if ($i = $value['moshi_week2_num']) {
                        $fund=M('sys')->getField('fund');
                        M('sys')->setInc('fund_total',$fund);
                        $data['fund_num']=$fund;
                        $data['addtime']=time();
                        $data['user_id']=$value['user_id'];
                        $data['items']='增加公益基金';

                        M('fund_record')->add($data);
                        }

                    }
                   
                

            }
            $gz=M('order_gz')->find();
            if ($gz) {
              $this->fahuan_cf();
            }

            $firstday = date("Y-m-01",$cur_time);
      $lastdayend = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day"));
      $lastdaybegin = date("Y-m-d 00:00:01",strtotime("$firstday +1 month -1 day"));

      if ($cur_time >=$lastdaybegin && $cur_time <= $lastdayend) {
             $cond['beyond_sj_id']=array('NEQ','NULL');   
             $list=M('user')->where($cond)->select();
             if ($list) {
                foreach ($list as $key => $value) {
                    if ($cur_time <= $value['beyond_end_time'] ) {
                    $map['addtime'] = array(array('egt',$lastdaybegin),array('elt',$lastdayend),'AND');
                    $map['user_id'] =$value['id'] ;
                    $totalnum=M('order_team')->where($map)->count();
                    M('user')->where('id='.$value['beyond_sj_id'])->setInc('cf_jifen',$totalnum*0.01);
                    }
                }
             }
        }


    }

     //满10单之后返还财富积分的逻辑
    public function fahuan_cf()
    {       
        $cur_time=time();
        unset($map);
        $map['gz_status']  = array('lt',12);
            $order_gz= M('order_gz')->where($map)->select();
            $addcf=M('jifen')->getField('jifen_cf_gz');
            $addcf=ceil($addcf/12);

            foreach ($order_gz as $key => $value) {
             
                    for ($i=1; $i <2; $i++) { 
                      $month[$i]=strtotime("+".$i."months", $value['addtime']);
                      $start = mktime(0,0,0,date("m",$month[$i]),date("d",$month[$i]),date("Y",$month[$i]));
                      $end = mktime(23,59,59,date("m",$month[$i]),date("d",$month[$i]),date("Y",$month[$i]));

                        if ($cur_time>=$start && $cur_time <$end) {
                                M('user')->where(array('id'=>$value['user_id']))->setInc('cf_jifen',$addcf);
                                M('order_gz')->where(array('id'=>$value['id']))->setInc('gz_status',1);

                            }


                    }
                 
            }

    }



}
